package com.ruoyi.apps.controller;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.apps.domain.AntAppsCategory;
import com.ruoyi.apps.service.IAntAppsCategoryService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.domain.Ztree;

/**
 * 应用分类Controller
 * 
 * @author lzh
 * @date 2023-01-15
 */
@Controller
@RequestMapping("/apps/category")
public class AntAppsCategoryController extends BaseController
{
    private String prefix = "apps/category";

    @Autowired
    private IAntAppsCategoryService antAppsCategoryService;

    @RequiresPermissions("apps:category:view")
    @GetMapping()
    public String category()
    {
        return prefix + "/category";
    }

    /**
     * 查询应用分类树列表
     */
    @RequiresPermissions("apps:category:list")
    @PostMapping("/list")
    @ResponseBody
    public List<AntAppsCategory> list(AntAppsCategory antAppsCategory)
    {
        List<AntAppsCategory> list = antAppsCategoryService.selectAntAppsCategoryList(antAppsCategory);
        return list;
    }

    /**
     * 导出应用分类列表
     */
    @RequiresPermissions("apps:category:export")
    @Log(title = "应用分类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(AntAppsCategory antAppsCategory)
    {
        List<AntAppsCategory> list = antAppsCategoryService.selectAntAppsCategoryList(antAppsCategory);
        ExcelUtil<AntAppsCategory> util = new ExcelUtil<AntAppsCategory>(AntAppsCategory.class);
        return util.exportExcel(list, "应用分类数据");
    }

    /**
     * 新增应用分类
     */
    @GetMapping(value = { "/add/{id}", "/add/" })
    public String add(@PathVariable(value = "id", required = false) Long id, ModelMap mmap)
    {
        if (StringUtils.isNotNull(id))
        {
            mmap.put("antAppsCategory", antAppsCategoryService.selectAntAppsCategoryById(id));
        }
        return prefix + "/add";
    }

    /**
     * 新增保存应用分类
     */
    @RequiresPermissions("apps:category:add")
    @Log(title = "应用分类", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(AntAppsCategory antAppsCategory)
    {
        return toAjax(antAppsCategoryService.insertAntAppsCategory(antAppsCategory));
    }

    /**
     * 修改应用分类
     */
    @RequiresPermissions("apps:category:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        AntAppsCategory antAppsCategory = antAppsCategoryService.selectAntAppsCategoryById(id);
        mmap.put("antAppsCategory", antAppsCategory);
        return prefix + "/edit";
    }

    /**
     * 修改保存应用分类
     */
    @RequiresPermissions("apps:category:edit")
    @Log(title = "应用分类", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(AntAppsCategory antAppsCategory)
    {
        return toAjax(antAppsCategoryService.updateAntAppsCategory(antAppsCategory));
    }

    /**
     * 删除
     */
    @RequiresPermissions("apps:category:remove")
    @Log(title = "应用分类", businessType = BusinessType.DELETE)
    @GetMapping("/remove/{id}")
    @ResponseBody
    public AjaxResult remove(@PathVariable("id") Long id)
    {
        return toAjax(antAppsCategoryService.deleteAntAppsCategoryById(id));
    }

    /**
     * 选择应用分类树
     */
    @GetMapping(value = { "/selectCategoryTree/{id}", "/selectCategoryTree/" })
    public String selectCategoryTree(@PathVariable(value = "id", required = false) Long id, ModelMap mmap)
    {
        if (StringUtils.isNotNull(id))
        {
            mmap.put("antAppsCategory", antAppsCategoryService.selectAntAppsCategoryById(id));
        }
        return prefix + "/tree";
    }

    /**
     * 加载应用分类树列表
     */
    @GetMapping("/treeData")
    @ResponseBody
    public List<Ztree> treeData()
    {
        List<Ztree> ztrees = antAppsCategoryService.selectAntAppsCategoryTree();
        return ztrees;
    }
}
